package name.jiaosha.air.company.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.OperateTip;
import name.jiaosha.air.company.domain.Company;
import name.jiaosha.air.company.domain.Department;
import name.jiaosha.air.company.domain.Employee;
import name.jiaosha.air.company.service.ICompanyService;
import name.jiaosha.air.constant.Sex;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.ali8ba.common.PinyinToolkit;

/**
 * 员工控制器
 * 
 * @author Administrator
 * 
 */
@Controller
public class EmployeeController {

	@Autowired
	private ICompanyService companyService;

	@RequestMapping("/admin/employeeList")
	public ModelAndView getMyEmployeeList() throws Exception {

		List<Employee> list = null;

		if (AppConstants.DEBUG) {
			list = new ArrayList<Employee>();
			for (int i = 0; i <= 20; i++) {
				Employee employee = new Employee();
				employee.setId(i);
				employee.setName("张学友" + i);
				employee.setNamePinyin(PinyinToolkit.cn2FirstSpell(employee
						.getName()));
				employee.setIdcard("3325281981000000");
				employee.setInJob("Y");
				if(i<=12){
					employee.setSex(Sex.Female);
				}else{
					employee.setSex(Sex.Male);
				}
			
				employee.setPhone("13777777");
				//employee.setStatus((short) 1);
				employee.setEmpID(String.valueOf(9527+i));
				list.add(employee);
			}

		} else {

			list = companyService.getEmployeeByComId(1);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", list);
		model.addAttribute("totalProperty", 30);
		return new ModelAndView("jsonView", model);

	}

	public List<Map<String, Object>> getEeployeeById(int id) {

		List<Map<String, Object>> list = null;
		list = new ArrayList<Map<String, Object>>();
		Map<String, Object> m = new HashMap<String, Object>();

		Employee employee = companyService.getEmployeeById(id);

		m.put("employee", employee);
		list.add(m);
		m = null;
//		List<Department> departments = companyService
//				.getDepartmentByComId(employee.getCompany().getId());
//		m = new HashMap<String, Object>();
//		m.put("departments", departments);

		list.add(m);
		return list;
	}

	public OperateTip create(Employee employee) {

		companyService.createEmployee(employee);

		OperateTip tip = new OperateTip("1", "操作成功");
		return tip;
	}

	public OperateTip update(Employee employee) {

		OperateTip tip = new OperateTip("1", "操作成功");
		return tip;
	}
	@RequestMapping("/admin/createEmployee")
	public ModelAndView createEmployee(){
		ModelMap model = new ModelMap();
		model.addAttribute("success", true);	
		return new ModelAndView("jsonView", model);
	}
	
	@RequestMapping("/admin/updateEmployee")
	public ModelAndView updateEmployee(){
		ModelMap model = new ModelMap();
		model.addAttribute("success", true);	
		return new ModelAndView("jsonView", model);
	}

}
